//
//  LHSunnyViewController.m
//  Project_B
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "LHSunnyViewController.h"



@interface LHSunnyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,LHPicDelegate>
@property (strong, nonatomic)UICollectionView *collection;//collectionView
@property (strong, nonatomic)UIButton *leftButton;//段子button
@property (strong, nonatomic)UIButton *rightButton;//图片button
@property (assign, nonatomic)BOOL isJoke;//是否为段子
@property (copy, nonatomic)NSString *str;
@property (strong, nonatomic)LHPicCollectionCell *cell1;
@property (strong, nonatomic)LHPictureDetailController *detail;
@end

@implementation LHSunnyViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //取消半透明
    self.navigationController.navigationBar.translucent = NO;
    self.isJoke = YES;
    [self createButtons];
    [self createCollection];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

}
#pragma mark ** 两个button
- (void)createButtons
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 15;
    self.navigationItem.titleView = bgView;
    
    //段子button
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_leftButton setTitle:@"段子" forState:(UIControlStateNormal)];
    [_leftButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _leftButton.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:_leftButton];
    //方法
    [_leftButton addTarget:self action:@selector(rightbtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(bgView);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    //图片button
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightButton setTitle:@"图片" forState:(UIControlStateNormal)];
    [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _rightButton.backgroundColor = [UIColor redColor];
    [bgView addSubview:_rightButton];
    //方法
    [_rightButton addTarget:self action:@selector(leftbtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_leftButton);
        make.left.mas_equalTo(_leftButton.mas_right);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
}
#pragma mark ** leftButton方法
- (void)leftbtnAction
{
    //改变collectionView的偏移量
    CGFloat collectRet = 1 * self.view.frame.size.width;
    [self.collection setContentOffset:CGPointMake(collectRet, 0) animated:YES];
    [UIView animateWithDuration:1 animations:^{
        
        //改变两button的颜色
        _leftButton.backgroundColor = [UIColor redColor];
        _rightButton.backgroundColor = [UIColor whiteColor];
        [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
    }];
}
#pragma mark ** rightButton方法
- (void)rightbtnAction
{
    //改变collectionView的偏移量
    CGFloat collectRet = 0 * self.view.frame.size.width;
    [self.collection setContentOffset:CGPointMake(collectRet, 0) animated:YES];
    [UIView animateWithDuration:1 animations:^{
        //改变两button的颜色
        _leftButton.backgroundColor = [UIColor whiteColor];
        _rightButton.backgroundColor = [UIColor redColor];
        [_leftButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }];
}
#pragma mark ** collectionView的创建
- (void)createCollection
{
    //collection创建
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor blueColor];
    //注册
    [_collection registerClass:[LHJokeCollectionCell class] forCellWithReuseIdentifier:@"LHJokeCollectionCell"];
    //添加
    [self.view addSubview:_collection];
    //按页滑动
    _collection.pagingEnabled = YES;
    //滑动反弹
    _collection.bounces = NO;
    //item之间距离
    layout.minimumLineSpacing = 0;
   
    //预设大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //背景色
    _collection.backgroundColor = [UIColor whiteColor];
    //代理
    _collection.delegate = self;
    _collection.dataSource = self;
    //注册
    [_collection registerClass:[LHJokeCollectionCell class] forCellWithReuseIdentifier:@"LHJokeCollectionCell"];
    [_collection registerClass:[LHPicCollectionCell class] forCellWithReuseIdentifier:@"LHPicCollectionCell"];


}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        LHJokeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LHJokeCollectionCell" forIndexPath:
                                      indexPath];

        return cell;
    }else{
        _cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"LHPicCollectionCell" forIndexPath:
                                     indexPath];
#pragma mark ** 签订代理人
        _cell1.LHPicDelegate = self;
        return _cell1;
    }
}

#pragma mark ** 实现图片代理方法
- (void)pushAction
{
    
    LHPictureDetailController *detail = [[LHPictureDetailController alloc] init];
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    detail.setid = _cell1.setid;
    detail.setname = _cell1.setname;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取当前页数
    NSInteger page = self.collection.contentOffset.x / self.collection.frame.size.width;
    if (0 == page) {
        [UIView animateWithDuration:1 animations:^{
        //改变两button的颜色
        _leftButton.backgroundColor = [UIColor whiteColor];
        _rightButton.backgroundColor = [UIColor redColor];
        [_leftButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }];
    }else{
        [UIView animateWithDuration:1 animations:^{
            //改变两button的颜色
        _leftButton.backgroundColor = [UIColor redColor];
        _rightButton.backgroundColor = [UIColor whiteColor];
        [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
